package com.fr.chicken.manager.logininfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fr.chicken.dao.LoginInfoDao;
import com.fr.chicken.dao.PlayerInfoDao;
import com.fr.chicken.facade.request.UserLoginRequsetDTO;
import com.fr.chicken.facade.request.UserRegisterRequsetDTO;
import com.fr.chicken.model.LoginInfo;
import com.fr.chicken.model.PlayerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
@Repository
public class LoginInfoManager {

    @Autowired
    private LoginInfoDao loginInfoDao;
    @Autowired
    private PlayerInfoDao playerInfoDao;

    public void createLoginInfo(UserRegisterRequsetDTO requsetDTO, PlayerInfo playerInfo) {
        LoginInfo loginInfo = JSONObject.parseObject(JSON.toJSONString(requsetDTO),LoginInfo.class);
        loginInfo.setLoginTime(new Date());
        loginInfo.setPlayerId(playerInfo.getId());
        int num1 = loginInfoDao.insertSelective(loginInfo);
        if (num1<1){
            throw new RuntimeException("插入注册信息失败");
        }
        playerInfo.setCreateInfo(loginInfo.getId());
        int num2 = playerInfoDao.updateByPrimaryKeySelective(playerInfo);
        if (num2<1){
            throw new RuntimeException("更新用户信息失败");
        }
    }
}
